/*
 * ForgetPassword.java
 *
 * Created on 6 Ноябрь 2006 г., 3:00
 */

package emailer;

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;

import crypto.*;

import javax.sql.*;
import emailer.EmailBean;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author alexog
 * @version
 */
public class ForgetPassword extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            Class.forName(com.mysql.jdbc.Driver.class.getName());
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String email = request.getParameter("email");
            String username = request.getParameter("username");
            
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?user=root&password=tolstyak");
            Statement stmt = conn.createStatement();
            ResultSet rs;
            String newpass = gen();
            String sqlmail = "SELECT * FROM gameusers WHERE username ='"+username+"'";
            String sqlupdate = "UPDATE gameusers SET password='"+crypto.UserPasswordCheck.putIntoDateBase(username,newpass)+
                                "' WHERE username ='"+username+"'";
            rs = stmt.executeQuery(sqlmail);
            boolean truem=false;
            if(rs.next()){
                if(email.equals(rs.getString("email")))
                    truem = true;
            }
            EmailBean emailer = new EmailBean();
            
            if(truem){
            
            int r = stmt.executeUpdate(sqlupdate);
            
            emailer.setSmtpHost("smtp.bk.ru");
            emailer.setTo(email);
            emailer.setPassword("tolstyak");
            emailer.setUsername("alexog");
            emailer.setFrom("alexog@bk.ru");
            emailer.setSubject("This is about your password in our system!");
            emailer.setContent("Your new password is "+newpass);
            out.println("we are hera");
            emailer.sendMessage();
            }else{
                
            }
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
                throw new ServletException(ex);
        }
    
    }
    
    private String gen() {
        String s = "";
        Random rGen = new Random();
        s += Integer.toHexString(rGen.nextInt());
        return s.substring(2, 7);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
